package org.iabako.server.servlet;

import org.iabako.shared.entity.enumeration.Language;
import org.iabako.shared.entity.enumeration.WebsiteDescriptionSection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by jose on 20/01/15.
 */
public class GoogleDescriptionServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String URLSuffix = request.getPathInfo();

        if (URLSuffix == null || URLSuffix.isEmpty()) {
            response.sendRedirect("/");
            return;
        }

        // URL PathInfo example :
        // URL/description/main/en
        // URL/description/what/en
        // URL/description/start/en
        String[] URLPathParameters = URLSuffix.split("/");

        if (URLPathParameters.length >= 3) {
            String section = URLPathParameters[1];
            String language = URLPathParameters[2];

            try {
                request.getSession().setAttribute("IABAKO_DESCRIPTION_SECTION", WebsiteDescriptionSection.valueOf(section));
                request.getSession().setAttribute("IABAKO_LANG", Language.valueOf(language));

            } catch (IllegalArgumentException e) {
                //Do nothing
            }
        }

        //we used forward for browser(and google engine?) transparency
        request.getRequestDispatcher("/iabako/").forward(request, response);
    }
}
